﻿namespace LS.Nc6.Web.Entry.Areas.Systems.Controllers
{
    /// <summary>
    /// 系统配置
    /// </summary>
    [Route("[area]/[controller]/[action]")]
    [Area("Systems")]
    [ApiDescriptionSettings("Default")]
    public class SysConfigurationController : BaseAdminController
    {
        #region SysConfigurationController
        private readonly ISysConfigurationService _configurationService;
        private readonly ICache _cache;
        public SysConfigurationController(ISysConfigurationService configurationService, IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed)
            :base(cacheOptions, resolveNamed)
        {
            _configurationService = configurationService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
        }
        #endregion

        #region 页面
        public ActionResult Index()
        {
            SysPower(SysMenuConstant.MENU_SYSCONFIGURATION_MANAGE, SysMenuConstant.FUN_SELECT, UserInfo.ID.ToString());
            return View();
        }
        public ActionResult Create()
        {
            SysPower(SysMenuConstant.MENU_SYSCONFIGURATION_MANAGE, SysMenuConstant.FUN_SELECT, UserInfo.ID.ToString());
            var model = new SysConfigurationEntity();
            model.OnOff = 1;//默认启用
            return View("Edit", model);
        }
        public async Task<ActionResult> Edit(int id)
        {
            SysPower(SysMenuConstant.MENU_SYSCONFIGURATION_MANAGE, SysMenuConstant.FUN_UPDATE, UserInfo.ID.ToString());
            var model = await _configurationService.GetByIdAsync(id);
            if (model == null)
            {
                return ErrorCustomMsg(ResponseHelper.NONEXISTENT);
            }
           
            return View(model);
        }
        #endregion

        #region  公共方法
        [HttpGet]
        public async Task<ResponseResultDataList> GetList()
        {
            string keyword = RequestHelper.FilterParam(Request.Query["title"]);
            var page = Request.Query["page"].ToString().GetQueryValueOrNull<int>(1);
            var limit = Request.Query["limit"].ToString().GetQueryValueOrNull<int>(20);
            string field = Request.Query["field"];
            string order = Request.Query["order"];

            var expable = Expressionable.Create<SysConfigurationEntity>();
            expable.AndIF(!string.IsNullOrEmpty(keyword), a => a.Title.Contains(keyword));
            var exp = expable.ToExpression();
            var strField = RequestHelper.FilterParam(field);
            var strOrder = RequestHelper.FilterParam(order);

            Expression<Func<SysConfigurationEntity, object>> expressionOrder;
            if (string.IsNullOrEmpty(strField) || string.IsNullOrEmpty(strOrder))
            {
                expressionOrder = (a) => new { a.Id };
            }
            else
            {
                //实现字段排序
                expressionOrder = (a) => new { a.CreateDate };
            }
            var result = await  _configurationService.PageList(exp, page, limit, expressionOrder);
            return new ResponseResultDataList { code = ResponseHelper.SEUCCESSCODE, message = "请求成功", data = result.Items, count = result.TotalCount };
        }



         /// <summary>
         /// 保存文件配置
         /// </summary>
         /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> SaveData([FromBody]SysConfigurationEntity model)
        {
            bool power = true;
            
            if ( await _configurationService.ExistsBm(model.Id.ToString(),model.Code))
            {
                return ResponseHelper.MessageResult(ResponseHelper.PARAMETER_BMYCZ);
            }
            long result = 0;
            if (model.Id > 0)
            {
                power = SysPower(SysMenuConstant.MENU_SYSCONFIGURATION_MANAGE, SysMenuConstant.FUN_UPDATE, UserInfo.ID.ToString(), false);
                if (!power)
                {
                    return ResponseHelper.MessageResult(ResponseHelper.PERMISSION_QXZU);
                }
                var editmodel = await _configurationService.GetByIdAsync(model.Id);
                CommonMethodHelper.MapperToModel(editmodel, model, null);
                result = await _configurationService.UpdateAsync(editmodel);
            }
            else
            {
                power = SysPower(SysMenuConstant.MENU_SYSCONFIGURATION_MANAGE, SysMenuConstant.FUN_ADD, UserInfo.ID.ToString(), false);
                if (!power)
                {
                    return ResponseHelper.MessageResult(ResponseHelper.PERMISSION_QXZU);
                }
              
                result = await _configurationService.InsertAsync(model);
            }
            return ResponseHelper.SaveResult(result);
        }


        [HttpPost]
        public async  Task<ResponseResult> Delete([FromBody]DeletDataInput input)
        {
            var r = SysPower(SysMenuConstant.MENU_SYSCONFIGURATION_MANAGE, SysMenuConstant.FUN_DELETE, UserInfo.ID.ToString(), false);
            if (!r)
            {
                return ResponseHelper.MessageResult(ResponseHelper.PERMISSION_WSCQX);
            }
            if (string.IsNullOrEmpty(input.Ids))
            {
                return ResponseHelper.MessageResult(ResponseHelper.PARAMETER_CSCW);
            }
            int result =await _configurationService.DeleteAsync(input.Ids.ToString());
            return ResponseHelper.DeleteResult(result);
        }

        #endregion


        #region 私有方法

        #endregion
    }
}